/*
 * ExcelBriniApp.java
 */

package excelbrini;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class ExcelBriniApp extends SingleFrameApplication implements IMessageManager {
    private String file_name1 = "";
    private String file_name2 = "";
    private String fileOutput = "";
    private int labelIndexFile1 = 7;
    private int labelIndexFile2 = 0;
    private String labelCodOrdFile1 = "";
    private String labelRagSocFile1 = "";
    private String labelValVASfile1 = "";
    private String labelValADSLfile1 = "";
    private String labelRagSocFile2 = "";
    private String labelCodOrdFile2 = "";
    private String labelImportoFile2 = "";
    private boolean debug = false;
    private ExcelBriniView view = null;
    /**
     * At startup create and show the main frame of the application.
     */
    @Override protected void startup() {
        view=new ExcelBriniView(this);
        show(view);
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of ExcelBriniApp
     */
    public static ExcelBriniApp getApplication() {
        return Application.getInstance(ExcelBriniApp.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {
        launch(ExcelBriniApp.class, args);
    }
    
    /**
     * @return the file_name1
     */
    public String getFile_name1() {
        return file_name1;
    }

    /**
     * @param file_name1 the file_name1 to set
     */
    public void setFile_name1(String file_name1) {
        this.file_name1 = file_name1;
    }

    /**
     * @return the file_name2
     */
    public String getFile_name2() {
        return file_name2;
    }

    /**
     * @param file_name2 the file_name2 to set
     */
    public void setFile_name2(String file_name2) {
        this.file_name2 = file_name2;
    }

    /**
     * @return the fileOutput
     */
    public String getFileOutput() {
        return fileOutput;
    }

    /**
     * @param fileOutput the fileOutput to set
     */
    public void setFileOutput(String fileOutput) {
        this.fileOutput = fileOutput;
    }
    public boolean readyToGo()
    {
    return !file_name1.isEmpty() && !file_name2.isEmpty() && !fileOutput.isEmpty();
    }

    /**
     * @return the labelIndexFile1
     */
    public int getLabelIndexFile1() {
        return labelIndexFile1;
    }

    /**
     * @param labelIndexFile1 the labelIndexFile1 to set
     */
    public void setLabelIndexFile1(int labelIndexFile1) {
        this.labelIndexFile1 = labelIndexFile1;
    }

    /**
     * @return the labelIndexFile2
     */
    public int getLabelIndexFile2() {
        return labelIndexFile2;
    }

    /**
     * @param labelIndexFile2 the labelIndexFile2 to set
     */
    public void setLabelIndexFile2(int labelIndexFile2) {
        this.labelIndexFile2 = labelIndexFile2;
    }

  

    /**
     * @return the labelCodOrdFile1
     */
    public String getLabelCodOrdFile1() {
        return labelCodOrdFile1;
    }

    /**
     * @param labelCodOrdFile1 the labelCodOrdFile1 to set
     */
    public void setLabelCodOrdFile1(String labelCodOrdFile1) {
        this.labelCodOrdFile1 = labelCodOrdFile1;
    }

    /**
     * @return the labelRagSocFile1
     */
    public String getLabelRagSocFile1() {
        return labelRagSocFile1;
    }

    /**
     * @param labelRagSocFile1 the labelRagSocFile1 to set
     */
    public void setLabelRagSocFile1(String labelRagSocFile1) {
        this.labelRagSocFile1 = labelRagSocFile1;
    }

    /**
     * @return the labelValVASfile1
     */
    public String getLabelValVASfile1() {
        return labelValVASfile1;
    }

    /**
     * @param labelValVASfile1 the labelValVASfile1 to set
     */
    public void setLabelValVASfile1(String labelValVASfile1) {
        this.labelValVASfile1 = labelValVASfile1;
    }

    /**
     * @return the labelValADSLfile1
     */
    public String getLabelValADSLfile1() {
        return labelValADSLfile1;
    }

    /**
     * @param labelValADSLfile1 the labelValADSLfile1 to set
     */
    public void setLabelValADSLfile1(String labelValADSLfile1) {
        this.labelValADSLfile1 = labelValADSLfile1;
    }

    /**
     * @return the labelRagSocFile2
     */
    public String getLabelRagSocFile2() {
        return labelRagSocFile2;
    }

    /**
     * @param labelRagSocFile2 the labelRagSocFile2 to set
     */
    public void setLabelRagSocFile2(String labelRagSocFile2) {
        this.labelRagSocFile2 = labelRagSocFile2;
    }

    /**
     * @return the labelCodOrdFile2
     */
    public String getLabelCodOrdFile2() {
        return labelCodOrdFile2;
    }

    /**
     * @param labelCodOrdFile2 the labelCodOrdFile2 to set
     */
    public void setLabelCodOrdFile2(String labelCodOrdFile2) {
        this.labelCodOrdFile2 = labelCodOrdFile2;
    }

    /**
     * @return the labelImportoFile2
     */
    public String getLabelImportoFile2() {
        return labelImportoFile2;
    }

    /**
     * @param labelImportoFile2 the labelImportoFile2 to set
     */
    public void setLabelImportoFile2(String labelImportoFile2) {
        this.labelImportoFile2 = labelImportoFile2;
    }
      int confrontaFile() {
          if(labelCodOrdFile1.isEmpty()) labelCodOrdFile1 = "ORDINE";
            if(labelRagSocFile1.isEmpty()) labelRagSocFile1 = "Ragione Sociale";
            if(labelValVASfile1.isEmpty()) labelValVASfile1="Valore VAS";
            if(labelValADSLfile1.isEmpty()) labelValADSLfile1="Provvigione ADSL";
            if(labelRagSocFile2.isEmpty()) labelRagSocFile2="ragioneSociale";
            if(labelCodOrdFile2.isEmpty()) labelCodOrdFile2="codiceOrdine";
            if(labelImportoFile2.isEmpty()) labelImportoFile2="importoCompenso";
        return MyExcel.fromFile2toFile1(this,file_name1, getLabelIndexFile1(), debug, file_name2, getLabelIndexFile2(), fileOutput,labelCodOrdFile1,labelRagSocFile1,labelValVASfile1,labelValADSLfile1,labelRagSocFile2,labelCodOrdFile2,labelImportoFile2);
    }

    public void showError(String str) {
        view.ShowError(str);
    }
    public void saveImpToFile()
    {
        try {
            BufferedWriter buff=new BufferedWriter(new FileWriter("conf.ini"));
            buff.write("nome colonna codice ordine file1:"+labelCodOrdFile1);
            buff.newLine();
            buff.write("nome colonna ragione sociale file1:"+labelRagSocFile1);
              buff.newLine();
            buff.write("nome colonna importo VAS file1:"+labelValVASfile1);
              buff.newLine();
            buff.write("nome colonna importo ADSL file1:"+labelValADSLfile1);
              buff.newLine();
            buff.write("numero riga intestazione file1:"+labelIndexFile1);
              buff.newLine();
            buff.write("nome colonna codice ordine file2:"+labelCodOrdFile2);
              buff.newLine();
            buff.write("nome colonna ragione sociale file2:"+labelRagSocFile2);
              buff.newLine();
            buff.write("nome colonna importo file2:"+labelImportoFile2);
              buff.newLine();
            buff.write("numero riga intestazione file2:"+labelIndexFile2);
              buff.newLine();
            buff.close();
        } catch (IOException ex) {
            Logger.getLogger(ExcelBriniApp.class.getName()).log(Level.SEVERE, null, ex);
            showError("impossibile salvare file configurazione");
        }
    }
    public void loadImpFromFile()
    {
        try {
            BufferedReader buff=new BufferedReader(new FileReader("conf.ini"));
            setLabelCodOrdFile1(buff.readLine().split(":")[1]);
            setLabelRagSocFile1(buff.readLine().split(":")[1]);
            setLabelValVASfile1(buff.readLine().split(":")[1]);
            setLabelValADSLfile1(buff.readLine().split(":")[1]);
            setLabelIndexFile1(Integer.parseInt(buff.readLine().split(":")[1]));
            setLabelCodOrdFile2(buff.readLine().split(":")[1]);
            setLabelRagSocFile2(buff.readLine().split(":")[1]);
            setLabelImportoFile2(buff.readLine().split(":")[1]);
            setLabelIndexFile2(Integer.parseInt(buff.readLine().split(":")[1]));
        } catch (FileNotFoundException ex) {
            if(labelCodOrdFile1.isEmpty()) labelCodOrdFile1 = "ORDINE";
            if(labelRagSocFile1.isEmpty()) labelRagSocFile1 = "Ragione Sociale";
            if(labelValVASfile1.isEmpty()) labelValVASfile1="Valore VAS";
            if(labelValADSLfile1.isEmpty()) labelValADSLfile1="Provvigione ADSL";
            if(labelRagSocFile2.isEmpty()) labelRagSocFile2="ragioneSociale";
            if(labelCodOrdFile2.isEmpty()) labelCodOrdFile2="codiceOrdine";
            if(labelImportoFile2.isEmpty()) labelImportoFile2="importoCompenso";
            saveImpToFile();
        }
        catch (Exception e)
        {
            showError("eccezione: "+e.getMessage());
            e.printStackTrace(System.err);
        }
    }
}
